﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblCust_BranchDAL:ItblCust_Branch
    {
        HKEA_CMSEntities model;
        tblCust_Branch branch;

        public tblCust_BranchDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblCust_BranchDAL GetInstance()
        {
            return new tblCust_BranchDAL();
        }

        public int InsertENT(tblCust_Branch ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblCust_Branch.AddObject(ent);
            return model.SaveChanges();
        }

        public tblCust_Branch InsertENTReturnENT(tblCust_Branch ent)
        {
            tblCust_Branch ret=new tblCust_Branch();
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblCust_Branch.AddObject(ent);
            if (model.SaveChanges() > 0)
                ret = model.tblCust_Branch.Where(a => a.CustBranchUID > 0).Where(a=>a.Deleted==0).OrderBy(a => a.CustBranchUID).ToList().Last();
                return ret;
        }

        public List<tblCust_Branch> GetListByENT(tblCust_Branch ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblCust_Branch.Where(a => a.CustBranchUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.CustBranchUID > 0)
                result = result.Where(a => a.CustBranchUID == ent.CustBranchUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<BranchListDetail> GetListDetailByENT(tblCust_Branch ent)
        {
            var result = from cust_Branch in model.tblCust_Branch
                         join address in model.tblAddresses
                         on cust_Branch.AddressUID equals address.AddressUID into tempAddress
                         from address in tempAddress.DefaultIfEmpty()

                         join info in model.tblContactInfoes
                         on cust_Branch.ContactInfoUID equals info.ContactInfoUID into tempInfo
                         from info in tempInfo.DefaultIfEmpty()
                         where cust_Branch.CustomerUID==ent.CustomerUID && cust_Branch.Deleted==ent.Deleted
                         select new BranchListDetail
                         {
                             CustBranchUID = cust_Branch.CustBranchUID,
                             AddressUID = cust_Branch.AddressUID == null ? 0 : (int)cust_Branch.AddressUID,
                             infoUID = cust_Branch.ContactInfoUID == null ? 0 : (int)cust_Branch.ContactInfoUID,
                             CustomerUID=cust_Branch.CustomerUID,
                             cust_BranchENT=cust_Branch
                         };
                return result.ToList();
        }

        public List<M_BranchListDetail> GetListByCustUID(List<int> ids, int custUID)
        {
            List<M_BranchListDetail> list = new List<M_BranchListDetail>();
            var ret = from branch in model.tblCust_Branch
                      where
                          branch.CustomerUID == custUID && branch.Deleted == 0
                      join info in model.tblContactInfoes
                        on branch.ContactInfoUID equals info.ContactInfoUID into infoENT
                      from info in infoENT.DefaultIfEmpty()
                      join address in model.tblAddresses
                      on branch.AddressUID equals address.AddressUID into addressENT
                      from address in addressENT.DefaultIfEmpty()
                      select new M_BranchListDetail
                      {
                          IsFromApp = false,
                          AddressUID = branch.AddressUID ?? 0,
                          CustBranchUID = branch.CustBranchUID,
                          InfoUID = branch.ContactInfoUID ?? 0,
                          BranchName_Chi = branch.BranchName_Chi,
                          BranchName_Eng = branch.BranchName_Eng,
                          NoOfEmployee = branch.NOfEmp ?? 0
                      };
            if (ids != null && ids.Count > 0)
                ret = ret.Where(a=>!ids.Contains(a.CustBranchUID));
            list = ret.ToList();
            return list;
        }

        public List<tblCust_Branch> GetListByENT(tblCust_Branch ent)
        {
            var result = model.tblCust_Branch.Where(a => a.CustBranchUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a=>a.CustomerUID==ent.CustomerUID);
            if (ent.CustBranchUID > 0)
                result = result.Where(a => a.CustBranchUID == ent.CustBranchUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public List<tblCust_Branch> GetListByListID(List<int> list)
        {
            var result = model.tblCust_Branch.Where(a => a.CustBranchUID > 0).Where(a => a.Deleted == 0)
                .Where(a=>list.Contains(a.CustBranchUID));
            return result.ToList();
        }

        public int GetCountByENT(tblCust_Branch ent)
        {
            var result = model.tblCust_Branch.Where(a => a.CustBranchUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.CustBranchUID > 0)
                result = result.Where(a => a.CustBranchUID == ent.CustBranchUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblCust_Branch ent)
        {
            branch = model.tblCust_Branch.Where(a=>a.CustBranchUID==ent.CustBranchUID).FirstOrDefault();
            if (branch != null)
            {
                branch.Deleted = ent.Deleted;
                branch.LastModifyBy = ent.LastModifyBy;
                branch.LastModifyDate = DateTime.Now;
                branch.AddressUID = ent.AddressUID;
                branch.BranchName_Chi = ent.BranchName_Chi;
                branch.BranchName_Eng = ent.BranchName_Eng;
                branch.ContactInfoUID = ent.ContactInfoUID;
                branch.CustomerUID = ent.CustomerUID;
                branch.IsHQ = ent.IsHQ;
                branch.NOfEmp = ent.NOfEmp;
                branch.WebSite = ent.WebSite;
            }
            return model.SaveChanges();
        }


        public int DeleteENT(tblCust_Branch ent)
        {
            var temp = model.tblCust_Branch.Where(a => a.CustBranchUID > 0);
            if (ent.CustBranchUID > 0)
                temp = temp.Where(a=>a.CustBranchUID==ent.CustBranchUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblCust_Branch.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
